//
//  RequireAcctionView.m
//  SSB
//
//  Created by Lyn on 16/9/21.
//  Copyright © 2016年 TNT. All rights reserved.
//

// 屏幕宽度
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenScale (ScreenWidth / 375.f)


#import "RequireAcctionView.h"
#import "RequireActionButton.h"

//pch后备
#import "UIView+PFAddtion.h"
#import "UIColor+GDBAddtion.h"
#import "UIImage+Color.h"

@interface RequireAcctionView ()<UICollectionViewDelegate,
    UICollectionViewDataSource>


@property (nonatomic, weak) UIImageView *titleImageView;

@property (nonatomic, weak) UILabel *titleLabel;

@property (nonatomic, weak) UIView *contentView;


@end



@implementation RequireAcctionView



+ (RequireAcctionView *)viewWithTitle:(NSString *)titleString titleImage:(UIImage *)titleImage itemArray:(NSArray *)itemArray  itemActionArray:(NSArray *)itemActionArray status:(BOOL)isExpand {
    RequireAcctionView *requireView = [[RequireAcctionView alloc]init];
    requireView.backgroundColor = [UIColor whiteColor];
    UIView *contentView = [[UIView alloc]init];
    if (isExpand) {
        requireView.frame = CGRectMake(0.f, 0.f, ScreenWidth , 179.f *ScreenScale);
        contentView.frame = CGRectMake(0.f, 0.f, ScreenWidth , 179.f *ScreenScale);
    } else {
        requireView.frame = CGRectMake(0.f, 0.f, ScreenWidth , 89.f *ScreenScale);
        contentView.frame = CGRectMake(0.f, 0.f, ScreenWidth , 89.f *ScreenScale);
    }
    
    contentView.backgroundColor = [UIColor whiteColor];
    [requireView addSubview:contentView];
    requireView.contentView = contentView;

    [requireView setTopAreaWithTitle:titleString image:titleImage btnStatus:isExpand];
    if (isExpand) {
    [requireView setBelowAreaWithItemArray:itemArray actionArray:itemActionArray];
    }
    return requireView;
}

#pragma mark--布局方法
- (void)setTopAreaWithTitle:(NSString *)titleString image:(UIImage *)titleImage btnStatus:(BOOL)status{
    UIImageView *titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14.f * ScreenScale, 14.f *ScreenScale, 42.f , 42.f)];
    titleImageView.image = titleImage;
    [self.contentView addSubview:titleImageView];
    self.titleImageView = titleImageView;
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(titleImageView.right + 15 * ScreenScale, 27.f *ScreenScale, ScreenWidth, 32.f)];
    titleLabel.text = titleString;
    titleLabel.textColor = [UIColor pf_colorWithHexString:@"#555555"];
    [titleLabel sizeToFit];
    [self.contentView addSubview:titleLabel];
    
    RequireActionButton *detailBtn = [RequireActionButton buttonWithType:UIButtonTypeCustom];
    detailBtn.frame = CGRectMake(0.f, 57.f * ScreenScale, 37.f, 21.f);
    detailBtn.right = ScreenWidth - 14.f;
    detailBtn.centerY = titleLabel.centerY;
    [detailBtn setImage:[UIImage imageNamed:@"require_btn_arrow_default"] forState:UIControlStateNormal];
    [detailBtn setImage:[UIImage imageNamed:@"require_btn_arrow_selected"] forState:UIControlStateSelected];
    [detailBtn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    detailBtn.selected = !status;
    [self.contentView addSubview:detailBtn];
    self.detailBtn = detailBtn;
    
}

- (void)setBelowAreaWithItemArray:(NSArray *)itemArray actionArray:itemActionArray {
    if (!itemArray.count) return;
    UIView *belowView = [[UIView alloc]initWithFrame:CGRectMake(0.f, self.titleImageView.bottom, ScreenWidth,32.f * ScreenScale +(((itemArray.count - 1) / 4) + 1) * 45.f *ScreenScale)];
    belowView.backgroundColor = [UIColor whiteColor];
    self.belowView = belowView;
    [self.contentView addSubview:self.belowView];
    
    for (int i = 0; i < itemArray.count; i++) {
        RequireActionButton * buttonItem = [RequireActionButton buttonWithType:UIButtonTypeCustom];
        buttonItem.frame = CGRectMake(14.f * ScreenScale +(i % 4)* 91.f * ScreenScale, 32.f * ScreenScale + (i / 4) * 47.f * ScreenScale, 74.f * ScreenScale, 30 * ScreenScale);
        buttonItem.layer.masksToBounds = YES;
        buttonItem.layer.cornerRadius = 15.f;
        [buttonItem setTitle:itemArray[i] forState:UIControlStateNormal];
        [buttonItem setTitleColor:[UIColor pf_colorWithHexString:@"#555555"] forState:UIControlStateNormal];
        buttonItem.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:13.f];
        [buttonItem setBackgroundImage:[UIImage imageFromContextWithColor:[UIColor pf_colorWithHexString:@"#cccccc"/*待考证*/]] forState:UIControlStateHighlighted];
        [buttonItem setBackgroundImage:[UIImage imageFromContextWithColor:[UIColor pf_colorWithHexString:@"#f7f7f7"]] forState:UIControlStateNormal];
        buttonItem.sortNum = i;
        [buttonItem addTarget:self action:@selector(turnToDetailView:) forControlEvents:UIControlEventTouchUpInside];
        [belowView addSubview:buttonItem];
    }
    self.contentView.height =( ((itemArray.count - 1)/ 4) + 1) * 45.f + 89.f;
    self.height =( ((itemArray.count - 1)/ 4) + 1) * 45.f + 89.f;
}

- (void)setLeftColor:(UIColor *)color {
    UIView *viewLeft = [[UIView alloc]initWithFrame:CGRectMake(0.f, 0.f, 6.f * ScreenScale, self.height)];
    viewLeft.backgroundColor = color;
    [self.contentView addSubview:viewLeft];
}

#pragma --mark 点击事件
- (void)btnAction {
    self.detailBtn.selected = !self.detailBtn.selected;
    if ([self.delegate respondsToSelector:@selector(changeCellHeight:indexNum:)]) {
        [self.delegate changeCellHeight:self.detailBtn.selected indexNum:self.detailBtn.rowNum];
    }

}

- (void)turnToDetailView:(RequireActionButton *)sender {
    if ([self.delegate respondsToSelector:@selector(goToDetailView:sortNum:rowNum:actionType:)]) {
        [self.delegate goToDetailView:sender.titleLabel.text  sortNum:sender.sortNum  rowNum:self.detailBtn.rowNum  actionType:sender.itemStr];
    }
    
}

@end
